<?php defined('SYSPATH') or die('No direct script access.');

class Controller_App extends Controller_Template
{
	public $template = 'templates/app';
	public $auth_required = FALSE;
	public $secure_actions = FALSE;
	
	public $appconfig;
	public $content;
	
	public function before() {
		parent::before();
		$this->appconfig = Kohana::config("appconfig");
		
		$this->template->maintitle = $this->appconfig['site']['maintitle'];
		$this->template->title = "";
		
		$this->template->meta_keywords = "";
		$this->template->meta_description = "";
		
		$this->template->styles = array();
		$this->template->scripts = array();
		
		$this->checkAuth();
	}
	
	public function messageView() {
		$this->content = $this->template->content = new View('app/default');
	}
	
	public function access_required() {
		$this->request->redirect('user/noaccess');
	}
	
	public function login_required() {
		$this->request->redirect('user/login/?puri=' . Request::current()->URI());
	}
	
	private function checkAuth() {
		$this->session = Session::instance();
		$action_name = Request::current()->action();
		
		if(($this->auth_required !== FALSE && Auth::instance()->logged_in($this->auth_required) === FALSE)
			|| (is_array($this->secure_actions) && array_key_exists($action_name, $this->secure_actions) && Auth::instance()->logged_in($this->secure_actions[$action_name]) === FALSE))
		{
			if(Auth::instance()->logged_in()) {
				$this->access_required();
			}else{
				$this->login_required();
			}
		}
	}
	
	public function action_working()
	{
		if($this->appconfig['site']['enabled']){
			$this->request->redirect("site/index");
		}
		
		$this->messageView();
		
		$this->template->styles = array(URL::base() . 'media/app/default.css' => 'screen');
		
		$this->template->title = "Suspensión momentánea";
		$this->content->message = "Estamos mejorando el servicio, en instantes podrás disfrutar de las nuevas modificaciones.";
	}
	
	public function action_404()
	{
		$this->messageView();
		$this->template->styles = array(URL::base() . 'media/app/default.css' => 'screen');
		$this->template->title = "404";
		$this->content->message = "La página que busca no existe.";
	}
}
	